package com.polaris.live.ui.live.widget

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.R
import com.polaris.live.common.ext.dp
import com.polaris.live.databinding.WidgetLiveNetworkBinding
import com.polaris.live.translation.R.string

/**
 * LiveNetworkView
 *
 * @author Created by lucas on 2023/11/24 09:44
 */
class LiveNetworkView(context: Context, attributeSet: AttributeSet) : LinearLayout(context, attributeSet) {

    private val mBinding = WidgetLiveNetworkBinding.inflate(LayoutInflater.from(context), this)

    private var mIsNetwork = true
    private var mIsConnected = true

    init {
        setBackgroundResource(R.drawable.r11_back_000000_30)
        gravity = Gravity.CENTER_VERTICAL
        orientation = HORIZONTAL
        setPadding(6.dp, 0, 6.dp, 0)

        visibility = GONE
    }

    /**
     * 设置网络状态
     */
    fun setNetworkState(isNetwork: Boolean, isConnected: Boolean) {
        if (!isNetwork) {
            visibility = VISIBLE
            mBinding.tvNetwork.text = StringUtils.getString(string.live_network_failure)
        } else {
            if (!isConnected) {
                visibility = VISIBLE
                mBinding.tvNetwork.text = StringUtils.getString(string.live_network_disconnect)
            } else {
                visibility = GONE
            }
        }

        mIsNetwork = isNetwork
        mIsConnected = isConnected
    }

    val isNetwork: Boolean
        get() = mIsNetwork

    val isConnected: Boolean
        get() = mIsConnected
}